package com.hzhh123;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.junit.Test;

/**
Consumer<T>:消费型接口
void accept(T t)

Supplier<T>:供给型接口 ,产生对象 
T get()

Function<T,R>:函数型接口  
对类型为T的对象应用操作，并返回结果。结果是R类型的对象。包含方法：  
R apply(T t);

Predicate<T>：断定型接口
确定类型为T的对象是否满足某约束，并返回boolean 值。包含方法    
boolean test(T t);
**/
public class TestLambda2 {

	public void handlerMsg(String msg,Consumer<String>consumer){
		consumer.accept(msg);
	}
	
	@Test
	public void test1(){
		handlerMsg("测试消费型接口", (s) ->System.out.println(s));
	}
	
	public List<Integer>getNums(int num,Supplier<Integer>sup){
		List<Integer>list=new ArrayList<>();
		for(int i=0;i<num;i++){
			list.add(sup.get());
		}
		return list;
	}
	
	/**
	 * 供给型接口
	 */
	@Test
	public void test2(){
		List<Integer> nums = getNums(10, () ->(int)(Math.random()*100));
		for (Integer num : nums) {
			System.out.println(num);
			
		}
	}
	
	public String operatorStr(String str,Function<String, String>fun){
		return fun.apply(str);
	}
	
	/**
	 * 函数式接口
	 */
	@Test
	public void test3(){
		String str = operatorStr("\t\t测试出", (s) ->s.trim());
		System.out.println(str);
		str=operatorStr("常常觉得口渴", (s)->s.substring(0, 3));
		System.out.println(str);
	}
	
	
	public List<String>hasStr(List<String>strlist,Predicate<String>p){
		List<String>resultList=new ArrayList<String>();
		for(String str:strlist){
			if(p.test(str)){
				resultList.add(str);
			}
		}
		return resultList;
	}
	/**
	 * 断言接口
	 */
	@Test
	public void test4(){
		List<String>aList=Arrays.asList("hhjk","ioio","www");
		List<String> list = hasStr(aList, (s)->s.length()>3);
		for (String s : list) {
			System.out.println(s);
		}
	}
}
